package com.fintech.third.service.youdun;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.fintech.common.enums.RespStatus;
import com.fintech.common.service.BaseServiceImpl;
import com.fintech.common.util.ApiResponse;
import com.fintech.common.util.PropertiesUtils;
import com.fintech.third.constant.LocalStatus;
import com.fintech.third.constant.ThirdMethodConstants;
import com.fintech.third.dao.youdun.EdcYdDeviceDetailMapper;
import com.fintech.third.dao.youdun.EdcYdDevicesListMapper;
import com.fintech.third.dao.youdun.EdcYdGraphDetailMapper;
import com.fintech.third.dao.youdun.EdcYdGraphPartnerUserInfoMapper;
import com.fintech.third.dao.youdun.EdcYdIdDetailMapper;
import com.fintech.third.dao.youdun.EdcYdLinkDeviceDetailMapper;
import com.fintech.third.dao.youdun.EdcYdLinkUserDetailMapper;
import com.fintech.third.dao.youdun.EdcYdLoanDetailMapper;
import com.fintech.third.dao.youdun.EdcYdLoanIndustryMapper;
import com.fintech.third.dao.youdun.EdcYdOtherLinkDeviceDetailMapper;
import com.fintech.third.dao.youdun.EdcYdRequestInfoMapper;
import com.fintech.third.dao.youdun.EdcYdUserFeaturesMapper;
import com.fintech.third.model.youdun.EdcYdDeviceDetail;
import com.fintech.third.model.youdun.EdcYdDevicesList;
import com.fintech.third.model.youdun.EdcYdGraphDetail;
import com.fintech.third.model.youdun.EdcYdGraphPartnerUserInfo;
import com.fintech.third.model.youdun.EdcYdIdDetail;
import com.fintech.third.model.youdun.EdcYdLinkDeviceDetail;
import com.fintech.third.model.youdun.EdcYdLinkUserDetail;
import com.fintech.third.model.youdun.EdcYdLoanDetail;
import com.fintech.third.model.youdun.EdcYdLoanIndustry;
import com.fintech.third.model.youdun.EdcYdOtherLinkDeviceDetail;
import com.fintech.third.model.youdun.EdcYdRequestInfo;
import com.fintech.third.model.youdun.EdcYdUserFeatures;
import com.fintech.third.utils.HttpClientUtils;
import com.fintech.third.utils.MD5Util;
import com.marvels.common.utils.MarvelsLogUtil;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 有盾
 * 
 * @author yangjy
 *
 */
@Service("ydService")
public class YdServiceImpl extends BaseServiceImpl implements YdService {
	@Autowired
	private EdcYdUserFeaturesMapper edcYdUserFeaturesMapper;
	@Autowired
	private EdcYdIdDetailMapper edcYdIdDetailMapper;
	@Autowired
	private EdcYdGraphDetailMapper edcYdGraphDetailMapper;
	@Autowired
	private EdcYdOtherLinkDeviceDetailMapper edcYdOtherLinkDeviceDetailMapper;
	@Autowired
	private EdcYdLinkUserDetailMapper edcYdLinkUserDetailMapper;
	@Autowired
	private EdcYdLinkDeviceDetailMapper edcYdLinkDeviceDetailMapper;
	@Autowired
	private EdcYdGraphPartnerUserInfoMapper edcYdGraphPartnerUserInfoMapper;
	@Autowired
	private EdcYdRequestInfoMapper edcYdRequestInfoMapper;
	@Autowired
	private EdcYdLoanDetailMapper edcYdLoanDetailMapper;
	@Autowired
	private EdcYdLoanIndustryMapper edcYdLoanIndustryMapper;
	@Autowired
	private EdcYdDevicesListMapper edcYdDevicesListMapper;
	@Autowired
	private EdcYdDeviceDetailMapper edcYdDeviceDetailMapper;
	
	/**
	 * 重新拉取一次有盾落库失败的数据
	 */
	public void runGetData(){
		// 应客户要求，重新拉取一次落库失败的数据
		List<Map<String, String>> result = edcYdRequestInfoMapper.queryLog();
			for (Map<String, String> map : result) {
				try {
					ApiResponse<Object> data = this.queryUserPortrait(map);
					
					if(data.getCode() == RespStatus.SUCCESS.getCode()){
						edcYdRequestInfoMapper.deleteFailLog(map.get("loanId"));
					}
				} catch (Exception e) {
					MarvelsLogUtil.getInstance().error("调用有盾接口时发生异常===="+e);
				}
			}
	}
	
	@Override
	public ApiResponse<Object> queryUserPortrait(Map<String, String> param) {
		MarvelsLogUtil.getInstance().info("==queryUserPortrait传入参数：" + JSON.toJSONString(param));
		boolean flag = true;
		ApiResponse<Object> response = new ApiResponse<Object>();
		// 判断接口是否停用
		if (PropertiesUtils.isTurnOffForSwitch("switch.yd0001")) {
			// RespStatus.DISABLE只有接口停用时才使用定义的这个参数，其它情况按原本的来
			response.setCodeAndMessage(RespStatus.DISABLE.getCode(), RespStatus.DISABLE.getMsg());
			return response;
		}
		Date requestStartDate = new Date();// 请求开始时间
		String status = LocalStatus.REQUEST_FAIL.getCode();// 默认请求失败
		String thirCode = null; // 第三方返回code
		String reqNo = "YD_" + System.currentTimeMillis() + "_" + RandomStringUtils.randomAlphanumeric(6);
		try {
			// 判断身份证号是否存在调用成功的记录
			Map<String, Object> queryByIdNoInfo = edcYdUserFeaturesMapper.queryByIdNoInfo(param.get("loanId"));
			if (queryByIdNoInfo != null) {
				MarvelsLogUtil.getInstance().info(param.get("loanId") + "已存在数据,不调用第三方！");
				flag = false;
				response.setCodeAndMessage(100045, param.get("loanId") + "已存在数据");
				return response;
			}
			// 参数校验
			if (StringUtils.isEmpty(param.get("idNo")) || StringUtils.isEmpty(param.get("loanId"))) {
				response.setCodeAndMessage(100001, "必要参数缺失");
				return response;
			}
			// 请求签名组装
			String secretkey = PropertiesUtils.getString("yd_security_key");
			JSONObject paramJson = new JSONObject();
			paramJson.put("id_no", param.get("idNo"));
			String requestSign = MD5Util.getMD5(paramJson + "|" + secretkey);

			// 请求地址组装
			String requestAddressUrl = PropertiesUtils.getString("yd_request_url");
			StringBuilder sbUrl = new StringBuilder();
			sbUrl.append(requestAddressUrl).append("pubkey/").append(PropertiesUtils.getString("yd_pub_key"))
					.append("/").append("product_code/").append("Y1001005").append("/").append("out_order_id/")
					.append(reqNo).append("/").append("signature/").append(requestSign);

			String sendHttpPost = "";
			try {
				// 开始请求
				sendHttpPost = HttpClientUtils.sendHttpPost(sbUrl.toString(), paramJson.toString());
				MarvelsLogUtil.getInstance().info("调用云慧眼用户画像接口返回数据:" + sendHttpPost);
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error("调用云慧眼用户画像接口时出现异常", e);
				response.setCode(RespStatus.REQ_ERROR.getCode());
				response.setMsg(RespStatus.REQ_ERROR.getMsg());
				return response;
			}

			JSONObject resultJson = JSONUtil.parseObj(sendHttpPost);
			JSONObject headerJson = (JSONObject) resultJson.get("header");
			JSONObject bodyJson = (JSONObject) resultJson.get("body");
			thirCode = headerJson.getStr("ret_code");
			if ("000000".equals(headerJson.getStr("ret_code"))) {
				if (bodyJson != null) {
					try {
						// 数据落库
						dataLibrary(bodyJson, reqNo, param.get("loanId"));
						status = LocalStatus.SUCCESS_WITH_DATA.getCode();
						response.setCodeAndMessage(RespStatus.SUCCESS.getCode(), RespStatus.SUCCESS.getMsg());
					} catch (Exception e) {
						MarvelsLogUtil.getInstance().error("有盾人面画像数据解析入库时出现异常", e);
						status = LocalStatus.IN_DATABASE_FAIL.getCode();
						response.setCode(RespStatus.DATABASE_ERROR.getCode());
						response.setMsg(RespStatus.DATABASE_ERROR.getMsg());
						return response;
					}
				} else {
					// 查询成功，但是返回结果为空
					status = LocalStatus.SUCCESS_WITHOUT_DATA.getCode();
					response.setCodeAndMessage(RespStatus.NODATA.getCode(), RespStatus.NODATA.getMsg());
					return response;
				}
			}
			return response;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("==queryUserPortrait出现异常", e);
			response.setCodeAndMessage(RespStatus.REQ_ERROR.getCode(), RespStatus.REQ_ERROR.getMsg());
			return response;
		} finally {
			if (flag) {
				saveDataQueryLogNew(reqNo, param.get("loanId"), null, requestStartDate, ThirdMethodConstants.YD0001,
						thirCode, status, "edc_yd_data_query_log", param.get("idNo"));
			}
		}
	}

	@Override
	public ApiResponse queryScore(Map<String, Object> param) {
		return null;
	}

	@SuppressWarnings("unchecked")
	public void dataLibrary(JSONObject bodyJson, String reqNo, String loanId) {

		String createdBy = "system";
		String updatedBy = "system";

		// 查询成功，返回结果不为空 OK
		String udOrderNo = bodyJson.getStr("ud_order_no");
		String lastModifiedTime = bodyJson.getStr("last_modified_time");
		JSONArray array = (JSONArray) bodyJson.get("user_features");
		if (array != null && !array.isEmpty()) {
			for (int i = 0; i < array.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map = (Map<String, Object>) array.get(i);
				String featureType = (String) map.get("user_feature_type");
				String modifiedDate = (String) map.get("last_modified_date");
				// 数据落库
				EdcYdUserFeatures userFeatures = new EdcYdUserFeatures();
				userFeatures.setReqNo(reqNo);
				userFeatures.setLoanId(loanId);
				userFeatures.setCreatedBy(createdBy);
				userFeatures.setCreatedDate(new Date());
				userFeatures.setUpdatedBy(updatedBy);
				userFeatures.setUpdatedDate(new Date());
				userFeatures.setUdOrderNo(udOrderNo);
				userFeatures.setLastModifiedTime(lastModifiedTime);
				userFeatures.setLastModifiedDate(modifiedDate);
				userFeatures.setUserFeatureType(featureType);
				edcYdUserFeaturesMapper.insertSelective(userFeatures);
			}
		}

		// 风险模型得分 OK

		String score = "";
		String riskEvaluation = "";
		JSONObject scoreDetailJson = (JSONObject) bodyJson.get("score_detail");
		if (scoreDetailJson != null) {
			score = scoreDetailJson.getStr("score");
			riskEvaluation = scoreDetailJson.getStr("risk_evaluation");
		}

		// 身份证详情 OK
		JSONObject idDetailJson = (JSONObject) bodyJson.get("id_detail");
		if (idDetailJson != null) {
			EdcYdIdDetail idDetail = new EdcYdIdDetail();
			idDetail.setBirthday(idDetailJson.getStr("birthday"));
			idDetail.setAddress(idDetailJson.getStr("address"));
			idDetail.setNames(idDetailJson.getStr("names"));
			idDetail.setGender(idDetailJson.getStr("gender"));
			idDetail.setNation(idDetailJson.getStr("nation"));
			idDetail.setProvince(idDetailJson.getStr("province"));
			idDetail.setCity(idDetailJson.getStr("city"));
			idDetail.setIssuingAgency(idDetailJson.getStr("issuing_agency"));
			idDetail.setIdNumberMask(idDetailJson.getStr("id_number_mask"));
			idDetail.setNameCredible(idDetailJson.getStr("name_credible"));
			idDetail.setScore(score);
			idDetail.setRiskEvaluation(riskEvaluation);
			idDetail.setReqNo(reqNo);
			idDetail.setLoanId(loanId);
			idDetail.setUdOrderNo(udOrderNo);
			idDetail.setCreatedBy(createdBy);
			idDetail.setCreatedDate(new Date());
			idDetail.setUpdatedBy(updatedBy);
			idDetail.setUpdatedDate(new Date());
			edcYdIdDetailMapper.insertSelective(idDetail);
		}

		// 图谱统计值 OK
		JSONObject graphDetailJson = (JSONObject) bodyJson.get("graph_detail");
		if (graphDetailJson != null) {
			EdcYdGraphDetail graphDetail = new EdcYdGraphDetail();
			graphDetail.setCreatedBy(createdBy);
			graphDetail.setCreatedDate(new Date());
			graphDetail.setUpdatedBy(updatedBy);
			graphDetail.setUpdatedDate(new Date());
			graphDetail.setReqNo(reqNo);
			graphDetail.setLoanId(loanId);
			graphDetail.setUdOrderNo(udOrderNo);
			graphDetail.setMobileCount(graphDetailJson.getStr("mobile_count"));
			graphDetail.setLinkUserCount(graphDetailJson.getStr("link_user_count"));
			graphDetail.setOtherLinkDeviceCount(graphDetailJson.getStr("other_link_device_count"));
			graphDetail.setPartnerUserCount(graphDetailJson.getStr("partner_user_count"));
			graphDetail.setBankcardCount(graphDetailJson.getStr("bankcard_count"));
			graphDetail.setLinkDeviceCount(graphDetailJson.getStr("link_device_count"));
			graphDetail.setUserHaveBankcardCount(graphDetailJson.getStr("user_have_bankcard_count"));
			edcYdGraphDetailMapper.insertSelective(graphDetail);
			// 其他关联设详情 OK
			JSONObject otherLinkDeviceDetailJson = (JSONObject) graphDetailJson.get("other_link_device_detail");
			if (otherLinkDeviceDetailJson != null) {
				EdcYdOtherLinkDeviceDetail deviceDetail = new EdcYdOtherLinkDeviceDetail();
				deviceDetail.setOtherFrandDeviceCount(otherLinkDeviceDetailJson.getStr("other_frand_device_count"));
				deviceDetail.setOtherLivingAttackDeviceCount(
						otherLinkDeviceDetailJson.getStr("other_living_attack_device_count"));
				deviceDetail.setReqNo(reqNo);
				deviceDetail.setLoanId(loanId);
				deviceDetail.setCreatedBy(createdBy);
				deviceDetail.setCreatedDate(new Date());
				deviceDetail.setUpdatedBy(updatedBy);
				deviceDetail.setUpdatedDate(new Date());
				deviceDetail.setUdOrderNo(udOrderNo);
				edcYdOtherLinkDeviceDetailMapper.insertSelective(deviceDetail);
			}

			// 关联用户详情 OK
			JSONObject linkUserDetailJson = (JSONObject) graphDetailJson.get("link_user_detail");
			if (linkUserDetailJson != null) {
				EdcYdLinkUserDetail linkUserDetail = new EdcYdLinkUserDetail();
				linkUserDetail.setPartnerMarkCount(linkUserDetailJson.getStr("partner_mark_count"));
				linkUserDetail.setCourtDishonestCount(linkUserDetailJson.getStr("court_dishonest_count"));
				linkUserDetail.setOnlineDishonestCount(linkUserDetailJson.getStr("online_dishonest_count"));
				linkUserDetail.setLivingAttackCount(linkUserDetailJson.getStr("living_attack_count"));
				linkUserDetail.setReqNo(reqNo);
				linkUserDetail.setLoanId(loanId);
				linkUserDetail.setUdOrderNo(udOrderNo);
				linkUserDetail.setCreatedBy(createdBy);
				linkUserDetail.setCreatedDate(new Date());
				linkUserDetail.setUpdatedBy(updatedBy);
				linkUserDetail.setUpdatedDate(new Date());
				edcYdLinkUserDetailMapper.insertSelective(linkUserDetail);
			}

			// 使用设备数详情 link_device_detail
			JSONObject linkDeviceDetailJson = (JSONObject) graphDetailJson.get("link_device_detail");
			if (linkDeviceDetailJson != null) {
				EdcYdLinkDeviceDetail linkDeviceDetail = new EdcYdLinkDeviceDetail();
				linkDeviceDetail.setFrandDeviceCount(linkDeviceDetailJson.getStr("frand_device_count"));
				linkDeviceDetail.setLivingAttackDeviceCount(linkDeviceDetailJson.getStr("living_attack_device_count"));
				linkDeviceDetail.setReqNo(reqNo);
				linkDeviceDetail.setLoanId(loanId);
				linkDeviceDetail.setUdOrderNo(udOrderNo);
				linkDeviceDetail.setUpdatedBy(updatedBy);
				linkDeviceDetail.setUpdatedDate(new Date());
				linkDeviceDetail.setCreatedBy(createdBy);
				linkDeviceDetail.setCreatedDate(new Date());
				edcYdLinkDeviceDetailMapper.insertSelective(linkDeviceDetail);
			}
			
			// 本商户内用户详情
			try {
				JSONArray graphPartnerUserInfoJson = (JSONArray) graphDetailJson.get("graph_partner_user_info");
				if (graphPartnerUserInfoJson != null && !graphPartnerUserInfoJson.isEmpty()) {
					List<EdcYdGraphPartnerUserInfo> ydGraphPartnerUserInfos = JSON.parseArray(
							graphDetailJson.get("graph_partner_user_info").toString(), EdcYdGraphPartnerUserInfo.class);
					for (EdcYdGraphPartnerUserInfo ydGraphPartnerUserInfo : ydGraphPartnerUserInfos) {
						ydGraphPartnerUserInfo.setReqNo(reqNo);
						ydGraphPartnerUserInfo.setLoanId(loanId);
						ydGraphPartnerUserInfo.setUdOrderNo(udOrderNo);
						ydGraphPartnerUserInfo.setUpdatedBy(updatedBy);
						ydGraphPartnerUserInfo.setUpdatedDate(new Date());
						ydGraphPartnerUserInfo.setCreatedBy(createdBy);
						ydGraphPartnerUserInfo.setCreatedDate(new Date());
					}
					edcYdGraphPartnerUserInfoMapper.insertSelective(ydGraphPartnerUserInfos);
				}
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error(loanId + "，有盾  - 本商户内用户详情 落库异常", e);
			}
		}

		try {
			// 申请行为
			if (bodyJson.get("request_info") != null) {
				EdcYdRequestInfo ydRequestInfo = JSON.parseObject(bodyJson.get("request_info").toString(), EdcYdRequestInfo.class);
				ydRequestInfo.setReqNo(reqNo);
				ydRequestInfo.setLoanId(loanId);
				ydRequestInfo.setUdOrderNo(udOrderNo);
				ydRequestInfo.setUpdatedBy(updatedBy);
				ydRequestInfo.setUpdatedDate(new Date());
				ydRequestInfo.setCreatedBy(createdBy);
				ydRequestInfo.setCreatedDate(new Date());
				edcYdRequestInfoMapper.insertSelective(ydRequestInfo);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(loanId + "，有盾  - 申请行为 落库异常", e);
		}
		
		// 借款详情 OK
		JSONObject loanDetailJson = (JSONObject) bodyJson.get("loan_detail");
		if (loanDetailJson != null) {
			EdcYdLoanDetail loanDetail = new EdcYdLoanDetail();
			loanDetail.setActualLoanPlatformCount(loanDetailJson.getStr("actual_loan_platform_count"));
			loanDetail.setActualLoanPlatformCount1m(loanDetailJson.getStr("actual_loan_platform_count_1m"));
			loanDetail.setActualLoanPlatformCount3m(loanDetailJson.getStr("actual_loan_platform_count_3m"));
			loanDetail.setActualLoanPlatformCount6m(loanDetailJson.getStr("actual_loan_platform_count_6m"));
			loanDetail.setLoanPlatformCount(loanDetailJson.getStr("loan_platform_count"));
			loanDetail.setRepaymentTimesCount(loanDetailJson.getStr("repayment_times_count"));
			loanDetail.setRepaymentPlatformCount(loanDetailJson.getStr("repayment_platform_count"));
			loanDetail.setLoanPlatformCount1m(loanDetailJson.getStr("loan_platform_count_1m"));
			loanDetail.setLoanPlatformCount3m(loanDetailJson.getStr("loan_platform_count_3m"));
			loanDetail.setLoanPlatformCount6m(loanDetailJson.getStr("loan_platform_count_6m"));
			loanDetail.setRepaymentPlatformCount1m(loanDetailJson.getStr("repayment_platform_count_1m"));
			loanDetail.setRepaymentPlatformCount3m(loanDetailJson.getStr("repayment_platform_count_3m"));
			loanDetail.setRepaymentPlatformCount6m(loanDetailJson.getStr("repayment_platform_count_6m"));
			loanDetail.setReqNo(reqNo);
			loanDetail.setLoanId(loanId);
			loanDetail.setUdOrderNo(udOrderNo);
			loanDetail.setCreatedBy(createdBy);
			loanDetail.setCreatedDate(new Date());
			loanDetail.setUpdatedBy(updatedBy);
			loanDetail.setUpdatedDate(new Date());
			edcYdLoanDetailMapper.insertSelective(loanDetail);
			// 行业借款详情 OK
			JSONArray loanIndustryArray = (JSONArray) loanDetailJson.get("loan_industry");
			if (loanIndustryArray != null && !loanIndustryArray.isEmpty()) {
				for (int i = 0; i < loanIndustryArray.size(); i++) {
					EdcYdLoanIndustry loanIndustry = new EdcYdLoanIndustry();
					Map<String, String> map = new HashMap<String, String>();
					map = (Map<String, String>) loanIndustryArray.get(i);
					loanIndustry.setName(map.get("name"));
					loanIndustry.setLoanPlatformCount(map.get("loan_platform_count"));
					loanIndustry.setRepaymentPlatformCount(map.get("repayment_platform_count"));
					loanIndustry.setRepaymentTimesCount(map.get("repayment_times_count"));
					loanIndustry.setActualLoanPlatformCount(map.get("actual_loan_platform_count"));
					loanIndustry.setReqNo(reqNo);
					loanIndustry.setLoanId(loanId);
					loanIndustry.setUdOrderNo(udOrderNo);
					loanIndustry.setCreatedBy(createdBy);
					loanIndustry.setCreatedDate(new Date());
					loanIndustry.setUpdatedBy(updatedBy);
					loanIndustry.setUpdatedDate(new Date());
					edcYdLoanIndustryMapper.insertSelective(loanIndustry);
				}
			}
		}

		// 设备信息 Ok
		JSONArray devicesListArray = (JSONArray) bodyJson.get("devices_list");
		if (devicesListArray != null && !devicesListArray.isEmpty()) {
			for (int i = 0; i < devicesListArray.size(); i++) {
				EdcYdDevicesList devicesList = new EdcYdDevicesList();
				Map<String, Object> map = new HashMap<String, Object>();
				map = (Map<String, Object>) devicesListArray.get(i);
				devicesList.setDeviceName((String) map.get("device_name"));
				devicesList.setDeviceId((String) map.get("device_id"));
				devicesList.setDeviceLastUseDate((String) map.get("device_last_use_date"));
				devicesList.setDeviceLinkIdCount((String) map.get("device_link_id_count"));
				devicesList.setCreatedBy(createdBy);
				devicesList.setCreatedDate(new Date());
				devicesList.setUpdatedBy(updatedBy);
				devicesList.setUpdatedDate(new Date());
				devicesList.setReqNo(reqNo);
				devicesList.setLoanId(loanId);
				devicesList.setUdOrderNo(udOrderNo);
				edcYdDevicesListMapper.insertSelective(devicesList);

				// 设备详情
				JSONObject deviceDetailJson = JSONUtil.parseObj(map.get("device_detail"));
				if (deviceDetailJson != null) {
					EdcYdDeviceDetail deviceDetail = new EdcYdDeviceDetail();
					deviceDetail.setCheatsDevice(deviceDetailJson.getStr("cheats_device"));
					deviceDetail.setIsRooted(deviceDetailJson.getStr("is_rooted"));
					deviceDetail.setFraudDevice(deviceDetailJson.getStr("fraud_device"));
					deviceDetail.setIsUsingProxyPort(deviceDetailJson.getStr("is_using_proxy_port"));
					deviceDetail.setAppInstalmentCount(deviceDetailJson.getStr("app_instalment_count"));
					deviceDetail.setNetworkType(deviceDetailJson.getStr("network_type"));
					deviceDetail.setLivingAttack(deviceDetailJson.getStr("living_attack"));
					deviceDetail.setReqNo(reqNo);
					deviceDetail.setLoanId(loanId);
					deviceDetail.setUdOrderNo(udOrderNo);
					edcYdDeviceDetailMapper.insertSelective(deviceDetail);
				}
			}
		}
	}

	/**
	 * 有盾测试数据
	 */
	public static void data() {// --------------

		Map<String, String> headerMap = new HashMap<String, String>();
		headerMap.put("req_time", "2017-11-05 15:04:56");
		headerMap.put("resp_time", "2017-11-05 15:04:57");
		headerMap.put("ret_code", "000000");
		headerMap.put("version", "4.1");
		headerMap.put("ret_msg", "操作成功");
		Map<String, Object> bodyMap = new HashMap<String, Object>();
		// --------------------------user_features start
		List<Map<String, String>> user_featuresMap = new ArrayList<Map<String, String>>();
		Map<String, String> user_features = new HashMap<String, String>();
		user_features.put("user_feature_type", "11");
		user_features.put("last_modified_date", "2017-06-28");
		user_featuresMap.add(user_features);
		Map<String, String> user_features1 = new HashMap<String, String>();
		user_features.put("user_feature_type", "5");
		user_features.put("last_modified_date", "2017-07-04");
		user_featuresMap.add(user_features1);
		Map<String, String> user_features2 = new HashMap<String, String>();
		user_features.put("user_feature_type", "17");
		user_features.put("last_modified_date", "2017-07-26");
		user_featuresMap.add(user_features2);
		bodyMap.put("user_features", user_featuresMap);
		// --------------------------user_features end
		bodyMap.put("last_modified_time", "2017-07-26 15:44:51");

		// --------------------------id_detail start
		Map<String, String> idDetailMap = new HashMap<String, String>();
		idDetailMap.put("birthday", "1974.12.04");
		idDetailMap.put("address", "安徽省合肥市庐阳区濉溪路**室");
		idDetailMap.put("names", "丁用,丁花 ");
		idDetailMap.put("gender", "男");
		idDetailMap.put("id_number_mask", "3401************14");
		idDetailMap.put("nation", "汉");
		idDetailMap.put("name_credible", "丁勇");
		bodyMap.put("id_detail", idDetailMap);
		// --------------------------id_detail end
		// --------------------------graph_detail start
		Map<String, Object> graph_detailMap = new HashMap<String, Object>();
		graph_detailMap.put("mobile_count", "1");
		graph_detailMap.put("link_user_count", "358");
		graph_detailMap.put("other_link_device_count", "2982");
		Map<String, String> graph_detailMap2 = new HashMap<String, String>();
		graph_detailMap2.put("partner_mark_count", "0");
		graph_detailMap2.put("court_dishonest_count", "0");
		graph_detailMap2.put("online_dishonest_count", "3");
		graph_detailMap2.put("living_attack_count", "2");
		graph_detailMap.put("link_user_detail", graph_detailMap2);
		graph_detailMap.put("user_have_bankcard_count", "1");
		Map<String, String> other_link_device_detail = new HashMap<String, String>();
		other_link_device_detail.put("other_frand_device_count", "3");
		other_link_device_detail.put("other_living_attack_device_count", "3");
		graph_detailMap.put("other_link_device_detail", other_link_device_detail);
		graph_detailMap.put("partner_user_count", "0");
		graph_detailMap.put("bankcard_count", "157");
		Map<String, String> link_device_detail = new HashMap<String, String>();
		link_device_detail.put("frand_device_count", "0");
		link_device_detail.put("living_attack_device_count", "2");
		graph_detailMap.put("link_device_count", "31");
		graph_detailMap.put("link_device_detail", link_device_detail);
		bodyMap.put("graph_detail", graph_detailMap);
		// --------------------------graph_detail end

		// --------------------------loan_detail start
		Map<String, Object> loan_detail = new HashMap<String, Object>();
		loan_detail.put("actual_loan_platform_count", "0");
		loan_detail.put("actual_loan_platform_count_3m", "0");
		loan_detail.put("actual_loan_platform_count_1m", "0");
		loan_detail.put("actual_loan_platform_count_6m", "0");
		loan_detail.put("loan_platform_count", "5");
		loan_detail.put("repayment_times_count", "0");
		loan_detail.put("repayment_platform_count_3m", "0");
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> loan_industry = new HashMap<String, String>();
		loan_industry.put("actual_loan_platform_count", "0");
		loan_industry.put("name", "小额现金贷");
		loan_industry.put("loan_platform_count", "3");
		loan_industry.put("repayment_times_count", "0");
		loan_industry.put("repayment_platform_count", "0");
		list.add(loan_industry);
		Map<String, String> loan_industry1 = new HashMap<String, String>();
		loan_industry1.put("actual_loan_platform_count", "0");
		loan_industry1.put("name", "分期行业");
		loan_industry1.put("loan_platform_count", "0");
		loan_industry1.put("repayment_times_count", "0");
		loan_industry1.put("repayment_platform_count", "0");
		list.add(loan_industry1);
		Map<String, String> loan_industry2 = new HashMap<String, String>();
		loan_industry2.put("name", "大学生分期");
		loan_industry2.put("loan_platform_count", "0");
		list.add(loan_industry2);
		Map<String, String> loan_industry3 = new HashMap<String, String>();
		loan_industry3.put("name", "电商分期");
		loan_industry3.put("loan_platform_count", "0");
		list.add(loan_industry3);
		Map<String, String> loan_industry4 = new HashMap<String, String>();
		loan_industry4.put("name", "旅游分期");
		loan_industry4.put("loan_platform_count", "0");
		list.add(loan_industry4);
		Map<String, String> loan_industry5 = new HashMap<String, String>();
		loan_industry5.put("name", "教育分期");
		loan_industry5.put("loan_platform_count", "0");
		list.add(loan_industry5);
		Map<String, String> loan_industry6 = new HashMap<String, String>();
		loan_industry6.put("name", "汽车分期");
		loan_industry6.put("loan_platform_count", "0");
		list.add(loan_industry6);
		Map<String, String> loan_industry7 = new HashMap<String, String>();
		loan_industry7.put("name", "租房分期");
		loan_industry7.put("loan_platform_count", "0");
		list.add(loan_industry7);
		Map<String, String> loan_industry8 = new HashMap<String, String>();
		loan_industry8.put("name", "农业消费");
		loan_industry8.put("loan_platform_count", "0");
		list.add(loan_industry8);
		Map<String, String> loan_industry9 = new HashMap<String, String>();
		loan_industry9.put("name", "医美分期");
		loan_industry9.put("loan_platform_count", "0");
		list.add(loan_industry9);
		loan_detail.put("loan_industry", list);
		loan_detail.put("loan_platform_count_3m", "4");
		loan_detail.put("loan_platform_count_6m", "4");
		loan_detail.put("repayment_platform_count_1m", "0");
		loan_detail.put("repayment_platform_count", "0");
		loan_detail.put("repayment_platform_count_6m", "0");
		loan_detail.put("loan_platform_count_1m", "2");
		bodyMap.put("loan_detail", loan_detail);
		// --------------------------loan_detail end
		// --------------------------score_detail start
		Map<String, String> score_detail = new HashMap<String, String>();
		score_detail.put("score", "99");
		score_detail.put("risk_evaluation", "极高风险");
		bodyMap.put("score_detail", score_detail);
		// --------------------------score_detail end

		// --------------------------devices_list start
		Map<String, Object> devices_list = new HashMap<String, Object>();
		devices_list.put("device_name", "iPhone 6 Plus(A1522/A1524/A1593)");
		devices_list.put("device_id", "97_e6469aff-0131-4b38-9fac-999bfa20b014");
		devices_list.put("device_link_id_count", "6");
		devices_list.put("device_last_use_date", "2017-05-27");
		Map<String, String> device_detail = new HashMap<String, String>();
		device_detail.put("is_rooted", "0");
		device_detail.put("network_type", "Wifi");
		devices_list.put("device_detail", device_detail);

		Map<String, Object> devices_list2 = new HashMap<String, Object>();
		devices_list2.put("device_name", "OPPO OPPO R9s");
		devices_list2.put("device_id", "97_8dcbb789-5bb7-41c9-9de3-6a746c9ccd77");
		devices_list2.put("device_link_id_count", "1");
		devices_list2.put("device_last_use_date", "2017-06-15");
		Map<String, String> device_detail2 = new HashMap<String, String>();
		device_detail2.put("is_rooted", "0");
		device_detail2.put("network_type", "Wifi");
		device_detail2.put("app_instalment_count", "33");
		device_detail2.put("cheats_device", "0");
		device_detail2.put("is_using_proxy_port", "0");
		devices_list2.put("device_detail", device_detail2);

		List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
		list2.add(devices_list);
		list2.add(devices_list2);
		bodyMap.put("devices_list", list2);
		// --------------------------devices_list end

		bodyMap.put("ud_order_no", "212806438823985152");
		JSONObject dataJson = new JSONObject();
		dataJson.put("body", bodyMap);
		dataJson.put("header", headerMap);
		// ---------------
	}
}
